package com.ly.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.Bis_beauty_config;
import com.ly.service.BeautyConfigService;
import com.ly.util.Constants;
import com.ly.util.JedisUtil;
import com.xbw.bean.Sys_User;

@Controller
public class BeautyconfigHandler {
	@Autowired
	private BeautyConfigService service;
	/**
	 * 查询颜值配置表
	 * @param config
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping("querybeautyconfig")
	public String querybeautyconfig(Bis_beauty_config config,Map<String, Object> map,@RequestParam(value="page",defaultValue="1")int page){
		PageHelper.startPage(page,5);
		List<Bis_beauty_config> list=service.querybeautyconfig(config);
		PageInfo<Bis_beauty_config> info=new PageInfo<>(list);
		map.put("info",info);
		map.put("list",list);
		map.put("config", config);
		return "ly/showbeautyconfig";
	}
	/**
	 * 删除颜值配置
	 * @param config_id
	 * @return
	 */
	@RequestMapping("del/{config_id}")
	public String del(@PathVariable("config_id")Integer config_id){
		System.out.println(config_id);
		service.del(config_id);
		return "redirect:/querybeautyconfig";
	}
	/**
	 * 根据id查询出一条记录传到模态框做添加
	 * @param config_id
	 * @param map
	 * @param res
	 */
	@RequestMapping("toupdate/{config_id}")
	@ResponseBody
	public void toupdate(@PathVariable("config_id")Integer config_id,Map<String, Object> map,HttpServletResponse res){
		ObjectMapper mapper = new ObjectMapper();
		try {
			String jsonStr=mapper.writeValueAsString(service.querybyid(config_id));
			getWirte(res, jsonStr);
		      
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}
	/**
	 * 根据id是否为空来判断是添加还是修改颜值配置
	 * @param config
	 * @param map
	 * @return
	 */
	@RequestMapping("insertupdateconfig")
	public String insertupdateconfig(Bis_beauty_config config,Map<String, Object> map,HttpSession session){
		System.out.println("configid========"+config.getConfig_id());
		if (config.getConfig_id()==null || config.getConfig_id().equals("")) {
			Sys_User user=(Sys_User) session.getAttribute("sysuser");
			config.setCreate_user_id(user.getUser_id());
			config.setModify_user_id(user.getUser_id());
			service.insertconfig(config);
			return "redirect:/querybeautyconfig";
		}else {
			service.updateByKey(config);
			return "redirect:/querybeautyconfig";
		}		
	}
	public void getWirte(HttpServletResponse resp,String data) {
		try {
			if(data!=null&&!data.equals("")) {
				PrintWriter	out = resp.getWriter();
				out.print(data);
				out.flush();
				out.close();
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	};


	/**
	 * 
	 * @param beautyNum  一元可兑换多少个颜值
	 * @param hasDay   有效期
	 * @return
	 */
	@RequestMapping("saveConfig")
	public String saveConfig(@RequestParam(defaultValue="0")int beautyNum,
			@RequestParam(defaultValue="0")int hasDay){
		JedisUtil.setString(Constants.BEAUTYNUM,beautyNum+"",-1);
		JedisUtil.setString(Constants.HAS_DAY,hasDay+"",-1);
		return "redirect:/querybeautyconfig";
	}
	
	@RequestMapping("getDataFromRedis")
	@ResponseBody
	public String getDataFromRedis(){
		String beauty=JedisUtil.getString(Constants.BEAUTYNUM,"0");
		String hasDay=JedisUtil.getString(Constants.HAS_DAY,"0");
		String str=beauty+":"+hasDay;
		return str;
		
	}
	
}
